package com.slw.caiji.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.slw.caiji.Vo.params.PageParams;
import com.slw.caiji.dto.SysRole;
import com.slw.caiji.dto.SysUser;
import com.slw.caiji.mapper.SysRoleMapper;
import com.slw.caiji.mapper.SysUserMapper;
import com.slw.caiji.service.SysRoleService;
import com.slw.caiji.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class SysRoleServiceImpl implements SysRoleService {


    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Override
    public Result getSysRoleList(PageParams pageParams) {
        final Page<SysRole> objectPage = new Page<>(pageParams.getCurrent(), pageParams.getPageSize());
        final IPage<SysRole> sysUserIPage = sysRoleMapper.listSysRole(objectPage);
        return Result.success(sysUserIPage);
    }


    @Override
    public Result addRole(SysRole sysRole) {
        final SysRole sysRole1 = new SysRole();
        sysRole1.setName(sysRole.getName());
        sysRole1.setRemark(sysRole.getRemark());
        sysRole1.setLastUpdateTime(new Date());
//        sysRole1.setLastUpdateBy(sysRole1.getLastUpdateBy()); // 存储修改人的id 等到登陆做好后再接入
        return Result.success(sysRoleMapper.insert(sysRole1));
    }

    @Override
    public Result delRole(String ids) {
        final String[] split = ids.split(",");
        for (String s : split) {
            sysRoleMapper.deleteById(s);
        }
        return Result.success(1);
    }

    @Override
    public Result updateRole(SysRole sysRole) {
        SysRole sysRole1 = new SysRole();
        sysRole1.setName(sysRole.getName());
        sysRole1.setRemark(sysRole.getRemark());
        sysRole1.setId(sysRole.getId());
        sysRole1.setLastUpdateTime(new Date());
//        sysRole1.setLastUpdateBy(sysRole1.getLastUpdateBy()); // 存储修改人的id 等到登陆做好后再接入
        return Result.success(sysRoleMapper.updateById(sysRole1));
    }

    @Override
    public Result detailRole(String id) {
        SysRole sysRole = sysRoleMapper.selectById(id);
        return Result.success(sysRole);
    }
}
